<?php

/**
 * File Asset Controller
 * Controller จัดการร Asset (CSS,JavaScript) ใช้คู่กับ MY_Output
 * เวลาเรียกใช้งาน ให้ดูตัวอย่างที่ view/admin/index_v
 * @package MainController
 * @subpackage fileAsset
 * @category main
 * @link MY_Output
 * @author BeYourCyber
 * @version 1.0
 */
class Files extends Controller {
    function Files() {
        parent::Controller();
        /*Set your own path here*/
        $this->_path = "./";
        $this->_cachetime = 0;
        
    }
    /**
     * ใช้เพื่อป้องกัน error
     *
     */
    function index() {
        echo "";
    }
    /**
     * เรียกใช้ เพื่อ minify + pack javascript รวมกัน และ ทำ cache ไฟล์
     * @param string $this->uri->segment(3); ชื่อ js เช่น a:j:swfobject แปลว่า ไฟล์ swfobject.js เก็บใน ./a/j/
     * @return cache_file locate in folder sys/cache
     */
    function js() {
        $offset = 60 * 60 * 24 * 3;
        $ExpStr = "Expires: " . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT";


        $this->output->set_header("Cache-Control: must-revalidate");
        $this->output->set_header($ExpStr);
        $this->output->set_header("Content-Type: text/javascript");  
        $this->output->cache($this->_cachetime);
        $segs = $this->uri->segment(3);
        $segs = explode('--',$segs);
        foreach ($segs as $segment) {
            $segment = str_replace(':', '/', $segment);
            $filepath = $this->_path.$segment.'.js';
            $data['tmp'] = '';
            if(file_exists($filepath)) {
                $data['tmp'] .= file_get_contents($filepath);
                //$data['tmp'] .= file_get_contents($filepath);

            }
            $this->load->view('file/echo_v',$data);
        }
    }
    /**
     * minify + pack css รวมกัน และ ทำ cache ไฟล์
     * @param string $this->uri->segment(3); ชื่อ css เช่น a:c:bee แปลว่า ไฟล์ bee.css เก็บใน ./a/c/
     * @return cache_file locate in folder sys/cache
     */
    function css() {
        $offset = 60 * 60 * 24 * 3;
        $ExpStr = "Expires: " . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT";


        $this->output->set_header("Cache-Control: must-revalidate");
        $this->output->set_header($ExpStr);
        $this->output->set_header("Content-Type: text/css");
        $this->output->cache($this->_cachetime);
        //sleep(1);
        
        $segs = $this->uri->segment(3);
        $segs = explode('--',$segs);
        foreach ($segs as $segment) {
            $segment = str_replace(':', '/', $segment);
            $filepath = $this->_path.$segment.'.css';
            $data['tmp'] = '';
            if(file_exists($filepath)) {
                $data['tmp'] .= $this->_minify(file_get_contents($filepath));
                //$data['tmp'] .= file_get_contents($filepath);

            }
            $this->load->view('file/echo_v',$data);
        }
    }
    /**
     * minify + pack text
     * @param string $data
     * @return cache_file locate in folder sys/cache
     */
    function _minify($data) {
    $data = preg_replace( '#/\*.*?\*/#s', '', $data );
    // remove single line comments, like this, from // to \\n
    $data = preg_replace('/(\/\/.*\n)/', '', $data);
    // remove new lines \\n, tabs and \\r
    $data = preg_replace('/(\t|\r|\n)/', '', $data);
    // replace multi spaces with singles
    $data = preg_replace('/(\s+)/', ' ', $data);
    //Remove empty rules
    $data = preg_replace('/[^}{]+{\s?}/', '', $data);
    // Remove whitespace around selectors and braces
    $data = preg_replace('/\s*{\s*/', '{', $data);
    // Remove whitespace at end of rule
    $data = preg_replace('/\s*}\s*/', '}', $data);
    // Just for clarity, make every rules 1 line tall
    $data = preg_replace('/}/', "}\n", $data);
    $data = str_replace( ';}', '}', $data );
    $data = str_replace( ', ', ',', $data );
    $data = str_replace( '; ', ';', $data );
    $data = str_replace( ': ', ':', $data );
    $data = preg_replace( '#\s+#', ' ', $data );

    return $data;
}
}
?>